﻿@model IEnumerable<Common.Views.OrdersView>

@{
    ViewBag.Title = "Electros Ltd | Orders";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Your Orders</h2>
<script type="text/javascript">

    function addrating(inc, id) {
        var param = {
            pId: id,
            rating: $('#rate' + inc + '').val()
        };
        $.ajax({
            url: '@Url.Action("AddRating")',
            type: "POST",
            datatype: "json",
            data: param,
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
            },
            success: function (data) {
                //This is done to get the data returned by the method (unwrapping the data)
                var msg = data.hasOwnProperty("already rated") ? data.d : data;

                if (msg == "already rated") {
                    alert("You have already rated this product!");
                    document.getElementById('divRate+' + inc + '').style.visibility = 'hidden';
                    document.getElementById('divRateSuccess+' + inc + '').style.visibility = 'visible';
                }
                else {
                    alert("Your rating has been successfully recorded!");
                    $('#rate' + inc + '').val("");
                    document.getElementById('divRate+' + inc + '').style.visibility = 'hidden';
                    document.getElementById('divRateSuccess+' + inc + '').style.visibility = 'visible';
                }
            }
        });
    }

    function addcomment(inc, id) {
        if ($("#comment" + inc + "").val() == "") {
            alert("Input a comment!");
        }
        else {
            var param = {
                pId: id,
                comment: $('#comment' + inc + '').val()
            };
            $.ajax({
                url: '@Url.Action("AddComment")',
                type: "POST",
                datatype: "json",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
                },
                success: function (data) {
                    if (data != null) {
                        alert("Comment has been successfully added!");
                        $('#comment' + inc + '').val("");
                        document.getElementById('divComment+' + inc + '').style.visibility = 'hidden';
                        document.getElementById('divCommentSuccess+' + inc + '').style.visibility = 'visible';
                    }
                }
            });
        }
    }

    function savedetails() {
        $.ajax({
            url: '@Url.Action("SaveDetails")',
            type: "POST",
            datatype: "json",
            data: null,
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
            },
            success: function (data) {
                if (data != null) {
                    alert("Details have been sent to you via e-mail!");
                }
            }
        });
    }

    function printdetails() {
        $.ajax({
            url: '@Url.Action("PrintDetails")',
            type: "GET",
            datatype: "json",
            data: null,
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
            },
            success: function (data) {
                if (data != null) {
                    window.location = "Orders/PrintDetails";
                }
            }
        });
    }
</script>

@if (Model.Count() > 0)
{
    <table>
        <tr>
            <th>Order ID</th>
            <th>Product Name</th>
            <th>Product Image</th>
            <th>Price</th>
            <th>Add Rating</th>
            <th>Add Comment</th>
            <th>More Details</th>
        </tr>

    @{
        int counter = 1;
    }

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.OrderID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ProductName)
            </td>
            <td>
                <img height="150px" width="150px" src= "@Url.Content(item.ImageLink)" alt="@Html.DisplayFor(modelItem => item.ImageLink)" />
            </td>
            <td>
                €@Html.DisplayFor(modelItem => item.Price)
            </td>
            @*Rate product*@
            <td> 
                <div style="display:inline-block">
                    <div id='divRate+@counter' style="display:inline">
                        @{
                            List<SelectListItem> ratings = new List<SelectListItem>();
                            for (int i = 1; i <= 10; i++)
                            {
                                ratings.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() });
                            }
         
                            @Html.Label("Rating:") <br />
                            @Html.DropDownList("rate" + counter + "", ratings) <br />
                        }
                        <button type="button" onclick="addrating(@counter, @item.ProductID)" id="btnRate">Add Rating</button>
                    </div><br />
                    <div id='divRateSuccess+@counter' style="visibility:hidden; display:inline; color:Red">
                        Thank you for rating this product!
                    </div>
                </div>
            </td>
            @*Comment on product*@
            <td>  
                <div style="display:inline-block">
                    <div id='divComment+@counter' style="display:inline">
                        @Html.Label("Comment:") <br />
                        @Html.TextArea("comment" + counter + "") <br />
                        <button type="button" onclick="addcomment(@counter, @item.ProductID)">Add Comment</button>
                    </div><br />
                    <div id='divCommentSuccess+@counter' style="visibility:hidden; display:inline; color:Red">
                        Thank you for commenting on this product!
                    </div>
                </div>
            </td>
            <td>
                @Ajax.ActionLink("View Product Details", "GetProductDetails", new { pId = item.ProductID, oId = item.OrderID },
                    new AjaxOptions
                    {
                        HttpMethod = "GET",
                        UpdateTargetId = counter.ToString(),
                        InsertionMode = InsertionMode.Replace
                    })
                    <br />
                <div id='@counter'></div>
            </td>
        </tr>
        { counter++; }
    }
    </table> <br />
    
    <div style="text-align: center;">
        <button type="button" onclick="printdetails()">Print Details</button>
        <button type="button" onclick="savedetails()">Save Details</button>
    </div>
}
else
{
    <div style="color: Red"><b>No products found</b></div>
}